package com.dayuan.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dayuan.constant.AjaxMsg;
import com.dayuan.service.Goods_typeService;
import com.dayuan.vo.Goods_typeVo;
import com.dayuan.vo.ResultVo;

//home页面商品列表(类目)
@Controller
@RequestMapping("/goods_type")
public class Goods_typeController {

	@Resource(name = "goods_typeService")
	private Goods_typeService gs;

	@Resource
	private RedisTemplate<String, Object> redisTemplate;

	@RequestMapping(value = "/select.im", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public ResultVo selcet() {
		ResultVo resultvo = new ResultVo();
		Object obj = null;
		try {
			List<Goods_typeVo> list = null;
			try {
				// redis获取类目
				obj = redisTemplate.opsForList().rightPop("listredis");
			} catch (Exception e) {
				System.out.println("没启动redis");
			}
			if (obj != null) {
				list = (List<Goods_typeVo>) obj;
			} else {
				list = gs.GetGoods_type(0);

				try {
					redisTemplate.opsForList().leftPush("listredis", list);
				} catch (Exception e) {
					System.out.println("没启动redis");
				}

			}
			resultvo.setCode(AjaxMsg.SUCCESS.getValue());
			resultvo.setData(list);
			return resultvo;
		} catch (Exception e) {
			e.printStackTrace();
			resultvo.setCode(AjaxMsg.FAIL.getValue());
			resultvo.setMsg("网络错误，请稍候再试");
			return resultvo;
		}

	}
}
